/*
 * ESBeans.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.exceptions.ProgrammingError;
import org.scriptme.ecmascript.interpreter.Evaluator;
import org.scriptme.ecmascript.interpreter.LocalClassLoader;

/**
 * Implements the beans loader.
 */
public class ESBeans extends ESLoader {

	/**
	 * Create the top level bean loader (object Bean).
	 * 
	 * @param evaluator
	 *            the evaluator
	 */
	public ESBeans(Evaluator evaluator) {
		super(evaluator);
	}

	/**
	 * Create a new bean loader or package prefix.
	 * 
	 * @param packageName
	 *            The extension of the package name
	 * @param previousPackage
	 *            Represents the higher level package names
	 * @param classLoader
	 *            the class loader to use for this loader
	 * @param evaluator
	 *            the evaluator
	 */
	public ESBeans(String packageName, ESBeans previousPackage,
			LocalClassLoader classLoader, Evaluator evaluator) {
		super(packageName, previousPackage, classLoader, evaluator);
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESLoader#getPrototype()
	 */
	public ESObject getPrototype() {
		throw new ProgrammingError("Cannot get prototype of Beans");
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getESClassName()
	 */
	public String getESClassName() {
		return "Beans";
	}

	// overrides
	// Getting a property dynamically creates a new Beans prefix object
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getProperty(java.lang.String,
	 *      int)
	 */
	public ESValue getProperty(String propertyName, int hash)
			throws EcmaScriptException {
		ESValue value = (ESValue) properties.get(propertyName, hash);
		if (value == null) {
			String packageName = buildPrefix();
			value = new ESBeans(propertyName, this, classLoader, evaluator);
			// Cache it for faster retrievial
			properties.put(propertyName, hash, false, false, value);
		}
		return value;
	}

	// overrides
	// Establish a bean classloader
	// The parameter is the directory or jar to load from
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#callFunction(org.scriptme.ecmascript.data.ESObject,
	 *      org.scriptme.ecmascript.data.ESValue[])
	 */
	public ESValue callFunction(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {
		if (previousPackage == null && classLoader == null) {
			LocalClassLoader classLoader = new LocalClassLoader();
			return new ESBeans(null, null, classLoader, evaluator);
		} else {
			throw new EcmaScriptException("Java class not found: '"
					+ buildPrefix() + "'");
		}
	}

	// overrides
	// instantiates a bean
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESLoader#doConstruct(org.scriptme.ecmascript.data.ESObject,
	 *      org.scriptme.ecmascript.data.ESValue[])
	 */
	public ESObject doConstruct(ESObject thisObject, ESValue[] arguments)
			throws EcmaScriptException {

		String beanName = buildPrefix();
		ESObject value = null;

		if (beanName == null) {
			throw new EcmaScriptException(
					"cannot create beans without a package name");
		}

		try {
			Object bean = classLoader.loadClass(beanName).newInstance();
			if (debugJavaAccess) {
				System.out.println(" ** Bean '" + beanName + "' created");
			}
			value = new ESWrapper(bean, evaluator, true);
		} catch (ClassNotFoundException e) {
			throw new EcmaScriptException("Bean '" + beanName + "' not found: "
					+ e);
		} catch (InstantiationException e) {
			throw new EcmaScriptException("IOexception loading bean '"
					+ beanName + "': " + e);
		} catch (IllegalAccessException e) {
			throw new EcmaScriptException(
					"IllegalAccessException loading bean '" + beanName + "': "
							+ e);
		}
		return value;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#getTypeofString()
	 */
	public String getTypeofString() {
		return "JavaBeans";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESObject#toDetailString()
	 */
	public String toDetailString() {
		return "ES:<" + getESClassName() + ":'" + buildPrefix() + "'"
				+ ((classLoader == null) ? "" : (",@" + classLoader)) + ">";
	}
}
